package com.zdp.controller;

import com.zdp.common.domain.ResultInfo;
import com.zdp.common.util.JsonUtils;
import com.zdp.common.util.ResultInfoUtil;
import com.zdp.config.RedisOperator;
import com.zdp.pojo.bo.ShopcartBO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sesshomaru
 * @date 2021/7/31 15:01
 * 购物车接口相关的api
 *
 * 这个分为用户登录和用户未登录两种情况
 * 1.登录时，需要将前端的cookie中的购物车信息和后端对于的redis中保存的购物车数据同步的更新
 * 2.未登录，只需将前端的cookie中的购物车信息更新
 */
@RestController
@RequestMapping("shopcart")
public class ShopcatController extends BaseController{

    @Autowired
    private RedisOperator redisOperator;

    /**
     * 添加商品到购物车
     * @param userId
     * @param shopcartBO
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/add")
    public ResultInfo add(
            @RequestParam String userId,
            @RequestBody ShopcartBO shopcartBO,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        if (StringUtils.isBlank(userId)) {
            return ResultInfoUtil.buildError("");
        }

        System.out.println(shopcartBO);

        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        // 1. 从redis中获取购物车信息
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNotBlank(shopcartJson)) {
            // redis中已经有购物车了
            shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
            // 判断购物车中是否存在已有的商品，如果有的话counts累加
            boolean isHaving = false;
            for (ShopcartBO sc : shopcartList) {
                String specId = sc.getSpecId();
                if (specId.equalsIgnoreCase(shopcartBO.getSpecId())) {
                    // 累加同规格商品的购买数量
                    sc.setBuyCounts(sc.getBuyCounts() + shopcartBO.getBuyCounts());
                    isHaving = true;
                    break;
                }
            }
            // 缓存不存在则直接添加
            if (!isHaving) {
                shopcartList.add(shopcartBO);
            }
        } else {
            shopcartList = new ArrayList<>();
            shopcartList.add(shopcartBO);
        }
        redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));
        return ResultInfoUtil.buildSuccess("");
    }

    /**
     * 从购物车中删除商品
     * @param userId
     * @param itemSpecId
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/del")
    public ResultInfo del(
            @RequestParam String userId,
            @RequestParam String itemSpecId,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return ResultInfoUtil.buildError("");
        }

        // 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端购物车中的商品

        String shopcartJson = redisOperator.get(FOODIE_SHOPCART + ":" + userId);
        List<ShopcartBO> shopcartList = null;
        if (StringUtils.isNotBlank(shopcartJson)) {
            // 获取redis中已经有的购物车
            shopcartList = JsonUtils.jsonToList(shopcartJson, ShopcartBO.class);
            for (ShopcartBO sc : shopcartList) {
                String tempSpecId = sc.getSpecId();
                if (tempSpecId.equals(tempSpecId)) {
                    shopcartList.remove(sc);
                    break;
                }
            }
        }
        // 覆盖现有的redis中的购物车
        redisOperator.set(FOODIE_SHOPCART + ":" + userId, JsonUtils.objectToJson(shopcartList));
        return ResultInfoUtil.buildSuccess("");
    }

}
